<template>
  <div class="change-psw">
    <el-form
      size="mini"
      ref="passForm"
      :model="passForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="passForm.username" />
      </el-form-item>
      <el-form-item label="原密码" prop="old_pass">
        <el-input v-model="passForm.old_pass" type="password" />
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="passForm.pass" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="check_pass">
        <el-input v-model="passForm.check_pass" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('passForm')"
          >确认</el-button
        >
        <el-button @click="resetForm('passForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reset } from "@/api/reset";
import { removeToken } from "@/utils/cookie";

export default {
  name: "Reset",
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.passForm.check_pass !== "") {
          this.$refs.passForm.validateField("check_pass");
        }
        callback();
      }
    };
    var validateCheckPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.passForm.check_pass !== "") {
          this.$refs.passForm.validateField("check_pass");
        }
        callback();
      }
    };
    return {
      passForm: {
        pass: "",
        check_pass: "",
        old_pass: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        check_pass: [{ validator: validateCheckPass, trigger: "blur" }],
        old_pass: [{ validator: validateOldPass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          reset(this.passForm).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: "修改密码成功,重新登录!",
              });
              this.$router.push("/login");
            } else {
              this.$message.error(res.data);
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.change-psw {
  padding: 20px;
  .el-input {
    width: 300px;
  }
}
</style>
